IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DeclineAdvanceFundRequest]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[DeclineAdvanceFundRequest]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



--DeclineAdvanceFundRequest 41359, '2009A057584', 'Reason', '6'

CREATE PROCEDURE [dbo].[DeclineAdvanceFundRequest] 
(  
	@fwkDomainUserId	INT,
	@PropNumber	VARCHAR(50),
	@Reason VARCHAR(50),
	@StatusType	INT
)  
AS 

SET NOCOUNT ON

		DECLARE @AFRId INT
		DECLARE @AFREventId INT

		BEGIN TRANSACTION
				
				SET @AFRId = (SELECT Id FROM dbo.AdvanceFundRequest WHERE PropNumber = @PropNumber)

				INSERT INTO dbo.AdvanceFundRequestComments (AdvanceFundRequestId, AdvanceFundRequestStatusTypeId, Comment, CommentedBy, DateCommented)
					VALUES (@AFRId, @StatusType, @Reason, @fwkDomainUserId, GETDATE())

					IF @@ERROR <> 0 
					BEGIN
						-- Rollback the transaction
						ROLLBACK
						RAISERROR ('Error in declining the Fund Request.', 16, 1)
						RETURN
					END

				INSERT INTO dbo.AdvanceFundRequestEventHistory (AdvanceFundRequestId, AdvanceFundRequestEventId, CreatedBy, DateCreated)
				VALUES (@AFRId, @StatusType, @fwkDomainUserId, GETDATE())

				IF @@ERROR <> 0 
				BEGIN
					-- Rollback the transaction
					ROLLBACK
					RAISERROR ('Error in declining the Fund Request.', 16, 1)
					RETURN
				END

				INSERT INTO dbo.AdvanceFundRequestWorkFlowStatus (AdvanceFundRequestId, AdvanceFundRequestStatusTypeId, DateStart, CreatedBy)
				VALUES (@AFRId, @StatusType, GETDATE(), @fwkDomainUserId)

				IF @@ERROR <> 0 
				BEGIN
					-- Rollback the transaction
					ROLLBACK
					RAISERROR ('Error in declining the Fund Request.', 16, 1)
					RETURN
				END

		COMMIT TRANSACTION




GO